package com.jielin.handling.controller.tsuspect;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jielin.handling.Annotation.MyPower;
import com.jielin.handling.bean.AppResult;
import com.jielin.handling.entity.suspectandcase.SuspectAndCase;
import com.jielin.handling.entity.tsuspect.*;
import com.jielin.handling.entity.virtual.Virtual;
import com.jielin.handling.entity.vo.PoliceVo;
import com.jielin.handling.entity.vo.TSuspecListVo;
import com.jielin.handling.exception.ResultEnum;
import com.jielin.handling.page.DataGrid;
import com.jielin.handling.redis.RedisTemplateX;
import com.jielin.handling.service.accesspath.AccessPathService;
import com.jielin.handling.service.tsuspect.TSuspectService;
import com.jielin.handling.util.ConstantUtil;
import com.jielin.handling.util.DateUtil;
import com.jielin.handling.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;


/**
 * @author chenzhang
 * @date 2019/3/14 - 9:06
 */
@CrossOrigin
@RestController
@RequestMapping("/suspect/")
@Slf4j
@MyPower(name = "办案中心首页", url = "/suspect")
public class TSuspectController {
    @Autowired
    private TSuspectService tSuspectService;
    @Autowired
    private RedisTemplateX redisTemplateX;
    @Autowired
    private AccessPathService accessPathService;

    /**
     * 初始化登录后民警信息
     *
     * @param policeId 警员id
     * @return
     */
    @MyPower(name = "初始化登录后民警信息", url = "/selectPolice")
    @RequestMapping("selectPolice")
    public AppResult selectPolice(String policeId) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        PoliceVo vo = tSuspectService.selectPolice(policeId);
        List<PoliceVo> list = tSuspectService.selectRecorder(policeId);
        List<String> powerPageList = accessPathService.findPowerPathPageByPoliceId(policeId);
        boolean power = accessPathService.findPowerIsBig(policeId);
        data.put("policeInfo", vo);
        data.put("equipmentInfo", list);
        data.put("powerPageList", powerPageList);
        data.put("isBig", power);
        result.setCode(200);
        result.setMsg("查询民警信息成功！");
        result.setData(data);
        return result;
    }

    /**
     * //废弃接口
     * 民警执法记录首页 // 查询结果整合到selectPolice()中
     */
//    @MyPower(name = "民警执法记录首页", url = "/selectRecorder")
    @RequestMapping("selectRecorder")
    public AppResult selectRecorder(String policeId) {
        log.info("policeId:{}", policeId);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<PoliceVo> list = tSuspectService.selectRecorder(policeId);
        data.put("value", list);
        result.setCode(200);
        result.setMsg("查询执法记录仪信息成功！");
        result.setData(data);
        return result;
    }

    /**
     * 办案区实时人数(涉案人员和民警)
     */
    @MyPower(name = "办案区实时人数(涉案人员和民警)", url = "/handNumber")
    @RequestMapping("handNumber")
    public AppResult handNumber(String handleUnitCode) {
        log.info("handleUnitCode:{}", handleUnitCode);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        Map map = tSuspectService.handNumber(handleUnitCode);
        data.put("value", map);
        result.setCode(200);
        result.setMsg("查询办案区实时人数成功！");
        result.setData(data);
        return result;
    }

    /**
     * 新增嫌疑人信息
     *
     * @param
     * @param
     * @return
     */
    @MyPower(name = "新增嫌疑人信息", url = "/updateSuspect")
    @PostMapping(value = "/updateSuspect", produces = "application/json;charset=UTF-8")
    public AppResult updateSuspect(@RequestBody JSONObject jsonObject) {
        System.out.print("===================>>" + jsonObject);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        TSuspec tSuspect = jsonObject.getObject("TSuspect", TSuspec.class);//前台传的对象
        JSONArray array = jsonObject.getJSONArray("listArray");//前台传的虚拟身份集合
        /*if (tSuspect.getAge()==null||tSuspect.getAge()<0||StringUtil.isEmpty(tSuspect.getCaseName())||StringUtil.isEmpty(tSuspect.getIdValue())
        ||StringUtil.isEmpty(tSuspect.getPersonnelType())||StringUtil.isEmpty(tSuspect.getPhone())) {
         result.setMsg("传递参数有误");
         return result;
        }*/
        JSONObject json = null;
        tSuspectService.deleteVitual(tSuspect.getId());//再次调用删除嫌疑人虚率身份
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                TSuspectVirtual tSuspectVirtual = new TSuspectVirtual();
                json = (JSONObject) array.get(i);
                tSuspectVirtual.setVirtualId(tSuspect.getId());
                tSuspectVirtual.setId(StringUtil.generatorUUID());
                tSuspectVirtual.setVirtualType(json.get("type").toString());
                tSuspectVirtual.setVirtualValue(json.get("value").toString());
                tSuspectService.insertVitual(tSuspectVirtual);
            }
        }

        log.info("jsonObject:{}", JSONObject.toJSONString(jsonObject));
        TSuspec suspectState = tSuspectService.selectStatue(tSuspect.getId());
        if(!"8".equals(suspectState.getSuspectState())) {
            tSuspect.setSuspectState("1");
        }else{
            tSuspect.setSuspectState(suspectState.getSuspectState());
        }
        tSuspect.setVirtualId(tSuspect.getId());
        int value = tSuspectService.updateSuspect(tSuspect);

        if (value > 0) {
            data.put("value", 1);
            result.setCode(200);
            result.setMsg("更新嫌疑人成功!");
            result.setData(data);
        } else {
            data.put("value", 0);
            result.setCode(-1);
            result.setMsg("更新嫌疑人失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 嫌疑人手环
     */
    @MyPower(name = "添加嫌疑人手环信息", url = "/addBracelet")
    @ResponseBody
    @RequestMapping("addBracelet")
    public AppResult addBracelet(@RequestBody TBracelet tBracelet) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("tBracelet:{}", JSONObject.toJSONString(tBracelet));
        if (tBracelet.getSuspectId() == null || tBracelet.getRssi() == null || StringUtil.isEmpty(tBracelet.getSuspectId()) ||
                StringUtil.isEmpty(tBracelet.getRssi())) {
            result.setMsg("传递参数有误");
            return result;
        }
        int value = tSuspectService.addBracelet(tBracelet);
        if (value > 0) {
            data.put("value", 1);
            result.setCode(200);
            result.setMsg("新增嫌疑人手环成功!");
            result.setData(data);
        } else {
            data.put("value", 0);
            result.setCode(-1);
            result.setMsg("新增嫌疑人手环失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 根据嫌疑人id查询嫌疑人信息
     */
    @MyPower(name = "根据嫌疑人id查询嫌疑人信息", url = "/selectById")
    @CrossOrigin
    @RequestMapping("selectById")
    public AppResult selectById(String id) {
        log.info("id:{}", id);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        TSuspec sus = tSuspectService.selectById(id);
        data.put("value", sus);
        result.setCode(200);
        result.setMsg("查询嫌疑人成功!");
        result.setData(data);
        return result;
    }

    /**
     * 人员切换查询
     */
    @MyPower(name = "人员切换查询", url = "/selectSuspect")
    @ResponseBody
    @RequestMapping("selectSuspect")
    public AppResult selectSuspect(@RequestBody JSONObject queryCondition) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("user query...");
        SuspectVo vo = new SuspectVo();
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        vo.setQueryCondition(queryCondition);
        log.info("vo.getPageNum==={}", vo.getPageNum());
        log.info("vo.getPageSize==={}", vo.getPageSize());
        vo.setPage(pageNum, pageSize);
        log.info("vo.getPageNum==={}", vo.getPageNum());
        log.info("vo.getPageSize==={}", vo.getPageSize());

        List<TSuspec> orderList = tSuspectService.selectSuspect(vo);
        //用PageInfo对结果进行包装
        PageInfo<TSuspec> pageInfo = new PageInfo<>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        data.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(data);
        return result;
    }

    /**
     * 统计各种状态人员信息
     */
    @MyPower(name = "统计各种状态人员信息", url = "/selectByStatus")
    @RequestMapping("selectByStatus")
    public AppResult selectByStatus(TSuspec suspec) {
        log.info("id:{}", suspec);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<TSuspec> list = tSuspectService.selectByStatus(suspec);
        data.put("value", list);
        result.setCode(200);
        result.setMsg("查询各种状态人员信息成功！");
        result.setData(data);
        return result;
    }

    /**
     * 嫌疑人头像拍照17b84
     */
    @MyPower(name = "嫌疑人头像拍照", url = "/savePhoto")
    @RequestMapping("/savePhoto")
    public AppResult savePhoto(MultipartFile file, String id, String fileString) {
        AppResult appResult = new AppResult();
        JSONObject data = new JSONObject();
        log.info("file:{}", JSONObject.toJSONString(file));
        log.info("id:{}", JSONObject.toJSONString(id));
        log.info("fileString:{}", JSONObject.toJSONString(fileString));
        //文件夹地址
        String folderPath = ConstantUtil.PHOTO_PATH + "suspect" + File.separator + "headpic" + File.separator;
        // 获取当前时间毫秒数拼接
        String trueFileName = String.valueOf(System.currentTimeMillis());
        //生成文件夹
        if(ConstantUtil.XSKYRESTORAGE){
            if (file == null&& fileString==null) {
                data.put("data", "没有相关的文件");
                return AppResult.builder().setCode(ResultEnum.FAIL.getCode()).setMsg("添加警告！").setData(data);
            }
            if (tSuspectService.savePhoto2(file, id, fileString)> 0) {
                return AppResult.builder().setCode(ResultEnum.SUCCESS.getCode()).setMsg("文件上传成功！");
            } else {
                return AppResult.builder().setCode(ResultEnum.FAIL.getCode()).setMsg("文件上传失败！(可能原因：1.上传文件为空)");
            }
        }else{
            File folderFile = new File(folderPath);
            if (!folderFile.exists()) {
                folderFile.mkdirs();
            }
            try {
                if (file != null) {

                    String fileName = file.getOriginalFilename();
                    String fileType = fileName.substring(fileName.lastIndexOf("."));
                    String path = folderPath + trueFileName + fileType;//磁盘存储路径

                    String filePath = File.separator + "suspect" + File.separator + "headpic" + File.separator + trueFileName + fileType;//数据库存储
                    FileOutputStream out = new FileOutputStream(path);
                    out.write(file.getBytes());
                    out.flush();
                    out.close();
                    //设置图片路径
                    int value = tSuspectService.savePhoto(filePath, id);
                    //返回给前台的图片路径，nginx地址，端口，，nginx映射，数据库存储路径
                    String returnPath = ConstantUtil.SERVER_URL + filePath;
                    if (value > 0) {
                        data.put("value", 1);
                        data.put("returnPath", returnPath);
                        appResult.setCode(200);
                        appResult.setMsg("添加嫌疑人头像成功!");
                        appResult.setData(data);
                    } else {
                        data.put("value", 1);
                        appResult.setCode(200);
                        appResult.setMsg("添加嫌疑人头像失败!");
                        appResult.setData(data);

                    }
                    return appResult;
                } else if (fileString != null && fileString != "") {//// 解码Base64上传图片
                    String picType = ".png";
                    String paths = folderPath + trueFileName + picType;

                    //Base64解码
                    BASE64Decoder decoder = new BASE64Decoder();
                    String base64String = fileString.substring(fileString.indexOf(",") + 1);// 去掉头部（不能解析）
//                log.info("Base64解码后:\n" + base64String);
                    byte[] b = decoder.decodeBuffer(base64String);
                    for (int i = 0; i < b.length; ++i) {
                        if (b[i] < 0) {//调整异常数据
                            b[i] += 256;
                        }
                    }
                    String filePath = File.separator + "suspect" + File.separator + "headpic" + File.separator + trueFileName + picType;//数据库存储

                    FileOutputStream out = new FileOutputStream(paths);
                    out.write(b);
                    out.flush();
                    out.close();

                    //设置图片路径
                    int value = tSuspectService.savePhoto(filePath, id);
                    //返回给前台的图片路径，nginx地址，端口，，nginx映射，数据库存储路径
                    String returnPath = ConstantUtil.SERVER_URL + filePath;
                    if (value > 0) {
                        data.put("value", 1);
                        data.put("returnPath", returnPath);
                        appResult.setCode(200);
                        appResult.setMsg("添加嫌疑人头像成功!");
                        appResult.setData(data);
                    } else {
                        data.put("value", 0);
                        appResult.setCode(-1);
                        appResult.setMsg("添加嫌疑人头像失败!");
                        appResult.setData(data);
                    }
                    return appResult;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            appResult.setCode(-1);
            appResult.setMsg("添加嫌疑人头像失败!");
            return appResult;
        }
    }

    @MyPower(name = "高拍仪拍照", url = "/highMeter")
    @RequestMapping("highMeter")
    public AppResult highMeter(MultipartFile file, String suspectId, String fileString) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("file:{}", JSONObject.toJSONString(file));
        log.info("id:{}", JSONObject.toJSONString(suspectId));
        log.info("fileString:{}", JSONObject.toJSONString(fileString));
        //文件夹地址
        String folderPath = ConstantUtil.PHOTO_PATH + "suspect" + File.separator + "highMeter" + File.separator;
        // 获取当前时间毫秒数拼接
        String trueFileName = String.valueOf(System.currentTimeMillis());
        //生成文件夹
        File folderFile = new File(folderPath);
        if (!folderFile.exists()) {
            folderFile.mkdirs();
        }
        try {
            if (file != null) {
                // 文件原名称
                String fileName = file.getOriginalFilename();
                //截取文件后缀
                String fileType = fileName.substring(fileName.lastIndexOf("."));
                //拼接存储文件路径
                String path = folderPath + trueFileName + fileType;//磁盘存储路径
                String filePath = File.separator + "suspect" + File.separator + "highMeter" + File.separator + trueFileName + fileType;//数据库存储
                FileOutputStream out = new FileOutputStream(path);
                out.write(file.getBytes());
                out.flush();
                out.close();
                //设置图片路径
                int value = tSuspectService.highMeter(filePath, suspectId);
                if (value > 0) {
                    date.put("value", 1);
                    date.put("path", filePath);
                    appResult.setCode(200);
                    appResult.setMsg("高拍仪拍照成功!");
                    appResult.setData(date);
                } else {
                    date.put("value", 1);
                    date.put("path", filePath);
                    appResult.setCode(200);
                    appResult.setMsg("高拍仪拍照失败!");
                    appResult.setData(date);

                }
                return appResult;
            } else if (fileString != null && fileString != "") {//// 解码Base64上传图片
                //String truefileType=fileString.substring(fileString.indexOf("/")+1,fileString.indexOf(";"));// 获取图片类型
                String fileType = ".png";
                String path = folderPath + trueFileName + fileType;

                //Base64解码
                BASE64Decoder decoder = new BASE64Decoder();
                String base64String = fileString.substring(fileString.indexOf(",") + 1);// 去掉头部（不能解析）
//                log.info("Base64解码后:\n" + base64String);
                byte[] b = decoder.decodeBuffer(base64String);
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {//调整异常数据
                        b[i] += 256;
                    }
                }
                String filePath = File.separator + "suspect" + File.separator + "highMeter" + File.separator + trueFileName + fileType;//数据库存储

                FileOutputStream out = new FileOutputStream(path);
                out.write(b);
                out.flush();
                out.close();

                //设置图片路径
                int value = tSuspectService.highMeter(filePath, suspectId);
                if (value > 0) {
                    date.put("value", 1);
                    date.put("path", filePath);
                    appResult.setCode(200);
                    appResult.setMsg("添加嫌疑人头像成功!");
                    appResult.setData(date);
                } else {
                    date.put("value", 0);
                    date.put("path", filePath);
                    appResult.setCode(-1);
                    appResult.setMsg("添加嫌疑人头像失败!");
                    appResult.setData(date);
                }
                return appResult;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        appResult.setCode(-1);
        appResult.setMsg("添加嫌疑人头像失败!");
        return appResult;
    }

    /**
     * 嫌疑人id生成
     */
    @MyPower(name = "人员id生成", url = "/creatId")
    @ResponseBody
    @RequestMapping("creatId")
    public AppResult creatId() {
        AppResult appResult = new AppResult();
        JSONObject data = new JSONObject();
        String id = StringUtil.generatorUUID();
        log.info("id:{}", JSONObject.toJSONString(id));
        int value = tSuspectService.creatId(id);
        if (value > 0) {
            data.put("value", id);
            appResult.setCode(200);
            appResult.setMsg("人员id生成成功!");
            appResult.setData(data);
        } else {
            data.put("value", id);
            appResult.setCode(-1);
            appResult.setMsg("人员id生成失败!");
            appResult.setData(data);
        }
        return appResult;
    }

    /**
     * 查询各种状态的人员个数
     */
    @MyPower(name = "查询各种状态的人员个数", url = "/selectState")
    @RequestMapping("selectState")
    public AppResult selectState(TSuspec tSuspec) {
        AppResult appResult = new AppResult();
        JSONObject data = new JSONObject();
        TSuspecListVo tslv = tSuspectService.selectState(tSuspec);// 状态列表
        List<TSuspec> list = tSuspectService.selectByStatus(tSuspec);// 全部人员信息头像状态
        int caseNum = tSuspectService.getCaseNumByPoliceId(tSuspec);// 案件数量
        data.put("tslv", tslv);
        data.put("list", list);
        data.put("caseNum", caseNum);
        appResult.setCode(200);
        appResult.setMsg("查询各种状态的人员个数成功！");
        appResult.setData(data);
        return appResult;
    }

    /**
     * 审讯室设备状态
     */
    @MyPower(name = "审讯室设备状态", url = "/deviceState")
    @RequestMapping("deviceState")
    public AppResult deviceState(String organizationId) {
        AppResult appResult = new AppResult();
        JSONObject data = new JSONObject();

        List<DecicesStateVo> list = tSuspectService.deviceState(organizationId);
        data.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询审讯室设备状态成功！");
        appResult.setData(data);
        return appResult;
    }

    /**
     * 查询所有未关联案件嫌疑人
     */
    @MyPower(name = "查询所有未关联案件嫌疑人", url = "/unRelated")
    @RequestMapping("unRelated")
    public AppResult unRelated(String policeId, String name, String idValue, String startDate, String endDate, int pageSize, int pageNum) {
        TSuspec suspec = new TSuspec();
        suspec.setPoliceId(policeId);
        suspec.setName(name);
        suspec.setIdValue(idValue);
        if (!StringUtil.isEmpty(startDate)) {
            suspec.setStartDate(DateUtil.getDateByBaseStr(startDate, DateUtil.DATE_FORMAT_MINUS));
        }
        if (!StringUtil.isEmpty(endDate)) {
            suspec.setEndDate(DateUtil.getDateByBaseStr(endDate, DateUtil.DATE_FORMAT_MINUS));
        }
        suspec.setPageSize(pageSize);
        suspec.setPageNum(pageNum);
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        List<TSuspec> orderList = tSuspectService.unRelated(suspec);
        //用PageInfo对结果进行包装
        PageInfo<TSuspec> pageInfo = new PageInfo<TSuspec>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        appResult.setCode(200);
        appResult.setMsg("query success");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 未关联人员更新
     */
    @MyPower(name = "未关联人员更新", url = "/updateRelatedCases")
    @RequestMapping("updateRelatedCases")
    public AppResult updateRelatedCases(@RequestBody TSuspec suspec) {
        log.info("suspec:{}", JSONObject.toJSONString(suspec));
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        int value = tSuspectService.updateRelatedCases(suspec);
        if (value > 0) {
            date.put("value", value);
            appResult.setCode(200);
            appResult.setMsg("更新未关联人员更新成功!");
            appResult.setData(date);
        } else {
            date.put("value", 0);
            appResult.setCode(-1);
            appResult.setMsg("更新未关联人员更新失败!");
            appResult.setData(date);
        }
        return appResult;
    }

    /**
     * 根据id查看嫌疑人的办案流程
     */
    @MyPower(name = "根据id查看嫌疑人的办案流程", url = "/selectStatue")
    @RequestMapping("selectStatue")
    public AppResult selectStatue(String id) {
        log.info("id:{}", JSONObject.toJSONString(id));
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        TSuspec value = tSuspectService.selectStatue(id);
        date.put("value", value.getSuspectState());
        appResult.setCode(200);
        appResult.setMsg("查看嫌疑人的办案流程成功!");
        appResult.setData(date);
        return appResult;

    }

    /**
     * 待上传文件列表（电子卷宗模块，需要民警id）
     */
    @MyPower(name = "待上传文件列表", url = "/auditFailed")
    @RequestMapping("auditFailed")
    public AppResult auditFailed(@RequestBody JSONObject queryCondition) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("queryCondition>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + JSONObject.toJSONString(queryCondition));
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        log.info("user query...");
        TSuspec suspec = new TSuspec();
        suspec.setQueryCondition(queryCondition);
        suspec.setPage(pageNum, pageSize);
        List<TSuspec> orderList = tSuspectService.auditFailed(suspec);
        //用PageInfo对结果进行包装
        PageInfo<TSuspec> pageInfo = new PageInfo<TSuspec>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        data.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(data);
        return result;
    }

    /**
     * 办案区实时状态
     */
    @MyPower(name = "办案区实时状态", url = "/handlingState")
    @RequestMapping("handlingState")
    public AppResult handlingState(int pageNum, int pageSize) {
        log.info("user query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        List orderList = tSuspectService.handlingState(pageNum, pageSize);
        //用PageInfo对结果进行包装
        PageInfo<TSuspec> pageInfo = new PageInfo<TSuspec>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 根据民警id案件类型统计列表
     */
    @MyPower(name = "根据民警id案件类型统计列表", url = "/countCase")
    @RequestMapping("countCase")
    public AppResult countCase(@RequestBody JSONObject queryCondition) {
        log.info("user query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        TSuspec tsuspect = new TSuspec();
        tsuspect.setQueryCondition(queryCondition);
        tsuspect.setPage(pageNum, pageSize);
        List<TSuspec> orderList = tSuspectService.countCase(tsuspect);
        PageInfo<TSuspec> pageInfo = new PageInfo<TSuspec>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 根据民警id查询案件类型统计图
     */
    @MyPower(name = "根据民警id查询案件类型统计图", url = "/casePic")
    @RequestMapping("casePic")
    public AppResult casePic(String policeId) {
        log.info("suspectId:{}", JSONObject.toJSONString(policeId));
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        Map<String, List<Object>> value = tSuspectService.casePic(policeId);
        date.put("value", value);
        appResult.setCode(200);
        appResult.setMsg("查询成功!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 根据案件查询嫌疑人的信息(name)
     */
    @MyPower(name = "根据案件查询嫌疑人的信息(name)", url = "/selectNameByCase")
    @RequestMapping("selectNameByCase")
    public AppResult selectNameByCase(@RequestBody String caseCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        List<String> name = tSuspectService.selectNameByCase(caseCode);
        date.put("value", name);
        appResult.setCode(200);
        appResult.setMsg("查询嫌疑人的信息成功!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 根据案件编号和民警编号查询民警关联案件嫌疑人(二次审讯)
     */
    @MyPower(name = "根据案件编号和民警编号查询民警关联案件嫌疑人(二次审讯)", url = "/selectByCaseCode")
    @RequestMapping("selectByCaseCode")
    public AppResult selectByCaseCode(@RequestBody TSuspec tSuspec) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("tSuspec:{}", JSONObject.toJSONString(tSuspec));
        Map map = tSuspectService.selectByCaseCode(tSuspec);
        date.put("list", map);
        appResult.setCode(200);
        appResult.setMsg("查询民警关联案件嫌疑人成功!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 嫌疑人其他在场人员信息
     */
    @MyPower(name = "嫌疑人其他在场人员信息", url = "/updateFiled")
    @RequestMapping("updateFiled")
    public AppResult updateFiled(@RequestBody TSuspec tSuspec) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("tSuspec:{}", JSONObject.toJSONString(tSuspec));
        int value = tSuspectService.updateFiled(tSuspec);
        if (value > 0) {
            date.put("value", 1);
            appResult.setCode(200);
            appResult.setMsg("更新嫌疑人其他在场人员信息成功!");
            appResult.setData(date);
        } else {
            date.put("value", 0);
            appResult.setCode(-1);
            appResult.setMsg("更新嫌疑人其他在场人员信息失败!");
            appResult.setData(date);
        }
        return appResult;
    }

    /**
     * 根据嫌疑人的姓名模糊查询
     *
     * @param suspectName  嫌疑人姓名
     * @param policeId     警员id
     * @param suspectState 人员状态
     * @return
     */
    @MyPower(name = "根据嫌疑人的姓名模糊查询", url = "/findByName")
    @RequestMapping("/findByName")
    public AppResult findByName(String suspectName, String policeId, String suspectState) {
        if (StringUtil.isEmpty(policeId)){
            return AppResult.builder().setCode(ResultEnum.FAIL.getCode()).setMsg("传入的policeId为null，请传值");
        }

        JSONObject date = new JSONObject();
        log.info("TSuspectController===>findByName(suspectName)根据嫌疑人姓名模糊查询");
        TSuspec ts = new TSuspec();
        ts.setSusName(StringUtil.escapeMysqlChars(suspectName));
        ts.setPoliceId(policeId);
        ts.setSuspectState(suspectState);

        date.put("value", tSuspectService.selectByStatus(ts));
        return AppResult.builder().setCode(ResultEnum.SUCCESS.getCode()).setMsg(ResultEnum.SUCCESS.getMsg()).setData(date);
    }

    /**
     * 案件编号和案件名称下拉框(未完结)
     *
     * @param unitCode 单位编号
     * @return
     */
    @MyPower(name = "获取未完结案件编号和案件名称", url = "/selectCodeAndName",isPublic = true)
    @RequestMapping("selectCodeAndName")
    public AppResult selectCodeAndName(String unitCode,String name) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("policeId:{}", JSONObject.toJSONString(unitCode));
        List<TSuspec> list = tSuspectService.selectCodeAndName(unitCode,name);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询案件编号和案件名称成功!");
        appResult.setData(date);
        return appResult;
    }


    /**
     * 查询已完结案件编号和案件名称下拉框(已完结)
     *
     * @param unitCode 单位编号
     * @return
     */
    @MyPower(name = "查询已完结案件编号和案件名称下拉框", url = "/selectCompleteCase", isPublic = true)
    @RequestMapping("selectCompleteCase")
    @ResponseBody
    public AppResult selectCompleteCase(String unitCode,String name) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        List<TSuspec> list = tSuspectService.selectCompleteCase(unitCode,name);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询已完结案件编号和案件名称下拉框成功!");
        appResult.setData(date);
        return appResult;
    }


    /**
     * 案件编号和案件名称下拉框(所有)
     *
     * @param unitCode 单位编号
     * @return
     */
    @MyPower(name = "获取所有案件编号和案件名称下拉框", url = "/selectAllCodeAndName", isPublic = true)
    @RequestMapping("selectAllCodeAndName")
    public AppResult selectAllCodeAndName(String unitCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("policeId:{}", JSONObject.toJSONString(unitCode));
        List<TSuspec> list = tSuspectService.selectAllCodeAndName(unitCode);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("获取所有案件编号和案件名称下拉框!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 根据单位编号查询所有民警信息(活动人员陪同民警下拉框)
     *
     * @param unitCode 单位编号
     * @return
     */
    @MyPower(name = "根据单位编号查询所有民警信息(活动人员陪同民警)", url = "/selectPoliceByUnitcode")
    @RequestMapping("selectPoliceByUnitcode")
    public AppResult select(String unitCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("unitCode:{}", JSONObject.toJSONString(unitCode));
        List<TSuspec> list = tSuspectService.selectPoliceByUnitcode(unitCode);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询民警信息成功!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 查询嫌疑人列表下拉框
     *
     * @param caseCode
     * @return
     */
    @MyPower(name = "查询嫌疑人列表", url = "/relatedSuspect")
    @RequestMapping("relatedSuspect")
    public AppResult relatedSuspect(String caseCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("caseCode:{}", JSONObject.toJSONString(caseCode));
        List<TSuspec> list = tSuspectService.relatedSuspect(caseCode);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询嫌疑人列表信息成功!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 根据案件编号查询案件关联的所有嫌疑人
     *
     * @param caseCode
     * @return
     */
    @MyPower(name = "根据案件编号查询案件关联的所有嫌疑人", url = "/selectSuspectByCaseCode")
    @RequestMapping("selectSuspectByCaseCode")
    public AppResult selectSuspectByCaseCode(String caseCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("caseCode:{}", JSONObject.toJSONString(caseCode));
        List<TSuspec> list = tSuspectService.selectSuspectByCaseCode(caseCode);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询嫌疑人列表信息成功!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * @param virtualId 嫌疑人虚拟身份id
     * @return
     */
    @MyPower(name = "嫌疑人虚拟身份id", url = "/selectVirtual")
    @RequestMapping("selectVirtual")
    public AppResult selectVirtual(String virtualId) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("caseCode:{}", JSONObject.toJSONString(virtualId));
        List<Virtual> list = tSuspectService.selectVirtual(virtualId);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询嫌疑人虚率信息成功!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 只查询单位下所有办案民警
     *
     * @param unitCode
     * @return
     */
    @MyPower(name = "查询所有办案民警", url = "/selectOnlyPolice", isPublic = true)
    @RequestMapping("selectOnlyPolice")
    public AppResult selectOnlyPolice(String unitCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("caseCode:{}", JSONObject.toJSONString(unitCode));
        List<PoliceVo> list = tSuspectService.selectOnlyPolice(unitCode);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询所有办案民警成功!");
        appResult.setData(date);
        return appResult;
    }


    /**
     * 根据案件编号查询该案件下的所有嫌疑人姓名等信息(警综嫌疑人表)
     *
     * @param unitCode
     * @return
     */
    @MyPower(name = "查询所有办案民警", url = "/selectSuspectnameByCaseCode", isPublic = true)
    @RequestMapping("selectSuspectnameByCaseCode")
    public AppResult selectSuspectnameByCaseCode(String caseCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("caseCode:{}", JSONObject.toJSONString(caseCode));
        List<TSuspec> list = tSuspectService.selectSuspectnameByCaseCode(caseCode);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("查询所有办案民警成功!");
        appResult.setData(date);
        return appResult;
    }


    /**
     * 根据案件编号查询该案件下的所有嫌疑人姓名等信息(警综嫌疑人表)
     *
     * @param unitCode
     * @return
     */
    @MyPower(name = "根据单位编号查询所有嫌疑人名称", url = "/selectSuspectnameByUnitcode", isPublic = true)
    @RequestMapping("selectSuspectnameByUnitcode")
    public AppResult selectSuspectnameByUnitcode(String unitCode) {
        AppResult appResult = new AppResult();
        JSONObject date = new JSONObject();
        log.info("caseCode:{}", JSONObject.toJSONString(unitCode));
        List<TSuspec> list = tSuspectService.selectSuspectnameByUnitcode(unitCode);
        date.put("value", list);
        appResult.setCode(200);
        appResult.setMsg("根据单位编号查询所有嫌疑人名称!");
        appResult.setData(date);
        return appResult;
    }

    /**
     * 新增嫌疑人信息
     *
     * @param
     * @param
     * @return
     */
    @MyPower(name = "新增嫌疑人信息", url = "/addSuspect", isPublic = true)
    @PostMapping(value = "/addSuspect", produces = "application/json;charset=UTF-8")
    public AppResult addSuspect(@RequestBody SuspectAndCase suspectAndCase) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        if (StringUtil.isEmpty(suspectAndCase.getName())|| StringUtil.isEmpty(suspectAndCase.getIdValue())) {
            result.setMsg("参数不能为空");
            return result;
        }

        int value = 0;
        String id = "";
        int updateValue = 0;
        //常规办案的设置
        suspectAndCase.setType("1");
        if (StringUtil.isEmpty(suspectAndCase.getId())) {
            id = StringUtil.generatorUUID();
            suspectAndCase.setId(id);
            value = tSuspectService.addSuspect(suspectAndCase);
        } else {
            int exist = tSuspectService.selectByCaseCodeAndIdValue(suspectAndCase);
            if (exist == 0) {
                value = tSuspectService.addSuspect(suspectAndCase);
            } else {
                updateValue = tSuspectService.updateSuspect2(suspectAndCase);
            }
        }

        if (value == 1) {
            data.put("value", 1);
            data.put("id", id);
            result.setCode(200);
            result.setMsg("新增嫌疑人成功!");
            result.setData(data);
        }
        if (value == 0) {
            data.put("value", 0);
            result.setCode(-1);
            result.setMsg("新增嫌疑人失败!");
            result.setData(data);
        }
        if (updateValue != 0) {
            data.put("value", 1);
            data.put("id", suspectAndCase.getId());
            result.setCode(200);
            result.setMsg("修改嫌疑人成功!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 根据嫌疑人id查询嫌疑人信息
     */
    @MyPower(name = "根据嫌疑人id查询嫌疑人信息", url = "/selectSuspectById")
    @CrossOrigin
    @RequestMapping("selectSuspectById")
    public AppResult selectSuspectById(String id) {
        log.info("id:{}", id);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        SuspectAndCase suspectAndCase = tSuspectService.selectSuspectById(id);
        data.put("suspectAndCase", suspectAndCase);
        result.setCode(200);
        result.setMsg("查询嫌疑人成功!");
        result.setData(data);
        return result;
    }

    @MyPower(name="根据单位编号查询案管中心审核已经通过的所有嫌疑人姓名",url = "/selectSuspectNameByHandleUnitCode")
    @RequestMapping("selectSuspectNameByHandleUnitCode")
    public AppResult selectSuspectNameByHandleUnitCode(String unitCode){
        AppResult result = new AppResult();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("TSuspec",tSuspectService.selectSuspectNameByHandleUnitCode(unitCode));
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(jsonObject);
        return result;
    }

    /**
     * 修改二次审讯状态
     */
    @MyPower(name = "修改二次审讯状态", url = "/updateSuspecByTwo",isPublic = true)
    @RequestMapping("updateSuspecByTwo")
    public AppResult updateSuspecByTwo(String id,String caseCode) {
        log.info("id:{}", id);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        tSuspectService.updateIsTwotrial(id,caseCode);
        data.put("value", 0);
        result.setCode(200);
        result.setMsg("修改二次审讯状态成功!");
        result.setData(data);
        return result;
    }

}

